# -*- coding:UTF-8 -*-
# 开发人员: limenghui
# 开发时间：2024-09-22 上午9:00
# 文件名称：
# 开发工具：pycharm


from time import sleep
import requests
import io
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import json
import re

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest'
    'Content-Type': 'application/json;charset=utf-8'
}


options = webdriver.EdgeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 屏蔽密码提示框
# prefs = {'credentials_enable_service':False,'profile.password_manager_enabled':False}
# 反爬虫特征处理
options.add_argument('disable-blink-features=AutomationControlled')
# options.add_argument('headless') # 设置option

options.add_experimental_option("prefs", {
    "download.default_directory": r"C:\Users\limenghui\PycharmProjects\CITIC_BANK\WORK",
    "download.prompt_for_download": False,
    "plugins.always_open_plugin_dir": True,
    "profile.default_content_settings.popups": 0,
})

# # 获取分支机构网站
# url = "http://www.pbc.gov.cn/rmyh/105226/105442/index.html"
# driver = webdriver.Edge(options=options)
# driver.get(url)
# driver.implicitly_wait(10)
# rhfz = driver.find_elements(By.XPATH, '//*[@id="8857"]/div[2]/table[10]/tbody/tr[2]/td/table[5]/tbody/tr/td/a')
# rh_website = {i.text: i.get_attribute('href') for i in rhfz} # 获取人行分支机构网站
# print(rh_website)
#
# gg_dict = {}
# for fz,url in rh_website.items():
#     driver = webdriver.Edge(options=options)
#     driver.get(url)
#     driver.implicitly_wait(10)
#     if fz == '上海市分行':
#         next = driver.find_element(By.LINK_TEXT, '通知与公告')
#     else:
#         next = driver.find_element(By.LINK_TEXT, '公告信息')
#     gg_dict[fz] = next.get_attribute('href')
#     # print(gg_dict)
#     driver.close()
#
# # 将字典保存为JSON文件
# with open('fz_gg_url.json', 'w', encoding='utf-8') as f:
#     json.dump(gg_dict, f, ensure_ascii=False, indent=4)
#
# # gg_dict = {'浙江省分行': 'http://hangzhou.pbc.gov.cn/hangzhou/125248/index.html'}
# # gg_dict = {'天津市分行': 'http://tianjin.pbc.gov.cn/fzhtianjin/113735/index.html'}
#
# # 黑龙江、吉林、上海报错
# gk_dict = {}
# for fz,url in gg_dict.items():
#     driver = webdriver.Edge(options=options)
#     driver.get(url)
#     driver.implicitly_wait(10)
#     gk_list = []
#     try:
#         count= 0
#         while count <= 1:
#             count += 1
#             rows = driver.find_elements(By.XPATH, '//*[@id="content_right"]//a')  # 人行通用
#             texts = driver.find_elements(By.XPATH, '//*[@id="content_right"]')  # 人行通用
#             if len(rows) == 0:
#                 rows = driver.find_elements(By.XPATH, '//*[@id="r_con"]//a') # 人行通用 江西、重庆
#                 texts = driver.find_elements(By.XPATH, '//*[@id="r_con"]')  # 人行通用
#             text = ''.join([i.text for i in texts])
#             dates = re.findall(r'\b\d{4}-\d{2}-\d{2}\b', text)
#             if datetime.strptime(dates[0], "%Y-%m-%d") >= datetime.strptime('2023-01-01',"%Y-%m-%d"):
#                 for row in rows:
#                     if '国库现金管理' in row.text:
#                         print(row.text)
#                         gk_list.append(row.get_attribute('href'))
#                         #print(row.get_attribute('href'))
#                     else:
#                         pass
#                 next= driver.find_element(By.LINK_TEXT,'下一页')
#                 next.click()
#             else:
#                 break
#         driver.close()
#         print('{}国库现金管理招中标条数：'.format(fz),len(gk_list))
#         gk_dict[fz] = gk_list
#     except Exception as e:
#         print(fz,"报错了！！！")
#         pass
#
gk_dict={}
#爬取上海
gk_list = []
url_sh = "http://shanghai.pbc.gov.cn/fzhshanghai/113574/index.html"
driver = webdriver.Edge(options=options)
driver.get(url_sh)
driver.implicitly_wait(10)
for i in range(6):
    driver.implicitly_wait(10)
    rows = driver.find_elements(By.XPATH, '//*[@id="twaincenter"]/div[3]/div/ul/li/a')  # 人行通用
    for row in rows:
        if '国库现金管理' in row.text:
            print(row.text)
            gk_list.append(row.get_attribute('href'))
            print(row.get_attribute('href'))
        else:
            pass
    next= driver.find_element(By.LINK_TEXT,'下一页')
    next.click()
gk_dict['上海市分行'] = gk_list
driver.close()



# 爬取吉林省
gk_list = []
url_jl = "http://changchun.pbc.gov.cn/changchun/124660/index.html"
driver = webdriver.Edge(options=options)
driver.get(url_jl)
driver.implicitly_wait(10)
for i in range(3):
    driver.implicitly_wait(10)
    rows = driver.find_elements(By.XPATH, '//*[@id="16012"]/div[2]/table/tbody/tr//a')  # 人行通用
    for row in rows:
        if '国库现金管理' in row.text:
            print(row.text)
            gk_list.append(row.get_attribute('href'))
            print(row.get_attribute('href'))
        else:
            pass
    next= driver.find_element(By.LINK_TEXT,'下一页')
    next.click()
gk_dict['吉林省分行'] = gk_list
driver.close()



# 爬取黑龙江
gk_list = []
url_hlj = "http://haerbin.pbc.gov.cn/haerbin/112690/index.html"
driver = webdriver.Edge(options=options)
driver.get(url_hlj )
driver.implicitly_wait(10)
for i in range(6):
    driver.implicitly_wait(10)
    rows = driver.find_elements(By.XPATH, '//*[@id="r_con"]//a')  # 人行通用
    for row in rows:
        if '国库现金管理' in row.text:
            print(row.text)
            gk_list.append(row.get_attribute('href'))
            print(row.get_attribute('href'))
        else:
            pass
    next= driver.find_element(By.LINK_TEXT,'下一页')
    next.click()
gk_dict['黑龙江省分行'] = gk_list
driver.close()



# 将字典保存为JSON文件
with open('fz_gk_url1.json', 'w', encoding='utf-8') as f:
    json.dump(gk_dict, f, ensure_ascii=False, indent=4)

